1183C - Computer Game - CodeForces Solution


binary search math *1400

Please click on ads to support us..

Python Code:

import sys
def input():
    return sys.stdin.readline().strip()
def ispossible(make,n,k,a,b):
                return make*a+b*(n-make)<k
for _ in range(int(input())):
    k,n,a,b=map(int,input().split())
    if n*b>=k:
        print("-1")
        continue
    l=0
    r=n 
    ans=0
    while l<=r:
        m= (r-l)//2 + l 
        if ispossible(m,n,k,a,b):
            l=m+1 
            ans=m 
        else:
            r=m-1 
    print(ans)
    
   	  		  	     		  		  			  	

C++ Code:

#include <iostream>
using namespace std;

long long int busca_binaria(long long int k, long long int n, long long int a, long long int b) {
    long long int max = n, min = 0, meio, aux, result;

    while (min <= max) {
        meio = (min + max) / 2;
        aux = n - meio;

        if (meio * a + aux * b < k) {
            result = meio;
            min = meio + 1;
        }
        else if (meio * a + aux * b >= k)
            max = meio - 1;
    }
    
    if (max != - 1)
        return result;
    else
        return max;
}

int main() {
    long long int consulta, k, n, a, b, aux;

    cin >> consulta;

    for (long long int i = 0; i < consulta; i++) {
        cin >> k;
        cin >> n;
        cin >> a;
        cin >> b;

        printf("%lld\n", busca_binaria(k, n, a, b));
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares